package com.pain.scaffold.common.utils;

import org.apache.poi.xwpf.usermodel.*;
  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.util.Iterator;  
import java.util.List;  
  
public class WordReader {  
  
    public static void main(String[] args) {  
        try {  
            String filePath = "D://test.docx"; // 替换为你的Word文件路径
            FileInputStream fis = new FileInputStream(filePath);  
            XWPFDocument document = new XWPFDocument(fis);  
  
            // 读取段落  
            List<XWPFParagraph> paragraphs = document.getParagraphs();  
            for (XWPFParagraph para : paragraphs) {  
                System.out.println(para.getText());  
            }  
  
            // 读取表格  
            List<XWPFTable> tables = document.getTables();
            for (XWPFTable table : tables) {  
                for (XWPFTableRow row : table.getRows()) {  
                    for (XWPFTableCell cell : row.getTableCells()) {  
                        System.out.println(cell.getText());  
                    }  
                }  
            }

            // 读取列表  
//            List<XWPFSDT> sdts = document.getSDTs();
//            for (XWPFSDT sdt : sdts) {
//                if (sdt.getContent().size() > 0) {
//                    XWPFParagraph p = (XWPFParagraph) sdt.getContent().get(0);
//                    System.out.println(p.getText());
//                }
//            }
  
            // 关闭文档和输入流  
            document.close();  
            fis.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}